library EMTransmitter initializer init requires LegacySystem


	private function Trig_Ion_Charger_Conditions takes nothing returns boolean
		// designator
		if ( GetSpellAbilityId() == 'A08V'  ) then
			return true

		// activator
		elseif (  GetSpellAbilityId() == 'A08W'  ) then
			return true

		// canceller
		elseif ( GetSpellAbilityId() == 'A009'  ) then
			return true
		endif

		return false
	endfunction

	private function playSoundEM takes location l returns nothing
	   call PlaySoundArea("EM_transmitter", l, 1000)
	endfunction

	private function damageTargetEM takes unit target, integer damage returns nothing
		 local location l = GetUnitLoc(target)
		 call DamageArea ( target, l, damage, 350)
		 call RemoveLocation(l)
		 set l = null
	endfunction

	private function slowTargetEM takes unit target returns nothing
		// creates dummy unit with a thunderclap spell and casts it.
		return
	endfunction

	private function Trig_Ion_Charger_Actions takes nothing returns nothing
		local integer spell = GetSpellAbilityId()
		local integer des = 'A08V'
		local integer act = 'A08W' 
		local integer can = 'A009'    
		local effect f
		local integer damage = 20000
		local unit target 


		if( spell  == can) then
			  set target = null
			  //call putUnit(null,"FO_ion_target",target)  
			  //call print("Target EM cancelled")
		elseif(spell == des) then
			  set target = GetSpellTargetUnit()
			  call putUnit(null,"FO_ion_target",target)  
			  call print("EM sattelites have aquired target: " + GetUnitName(target))
		elseif(spell == act) then
			  set target = getUnit(null,"FO_ion_target")
			  if(target == null) then
				   return
			  endif
			  call TriggerSleepAction(1)
			  call AddSpecialEffectTargetUnitBJ( "overhead", target, "Abilities\\Spells\\NightElf\\Starfall\\StarfallCaster.mdl" )
			  set f = GetLastCreatedEffectBJ()
			  call slowTargetEM(target)
			  call TriggerSleepAction(3)
			  call damageTargetEM(target,damage)
			  call DestroyEffect( f )
		endif
		set f = null
		set target = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_Ion_Charger_Conditions ) )
		call TriggerAddAction( t, function Trig_Ion_Charger_Actions )
	endfunction

endlibrary